package org.example.Imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.dao.TemplateDao;
import org.example.TemplateService;
import org.example.domain.Question;
import org.example.domain.Teacher;
import org.example.domain.Template;
import org.example.pageParam.CommonData;
import org.example.vo.PageVO;
import org.example.vo.QuestionVO;
import org.example.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TemplateServiceImp implements TemplateService {
    @Autowired
    private TemplateDao templateDao;
    @Override
    public QuestionVO reverseVO(Question question,int currentQuestionNumber) {
        QuestionVO questionVO = new QuestionVO();
        if (question.getType().equals("单选题") || question.getType().equals( "多选题")){
            String[] options = question.getOptions().split(CommonData.COMMON_SEPARATOR);
            String[] answers = new String[options.length];
            String[] tempAnswers = question.getAnswer().split(CommonData.COMMON_SEPARATOR);//可能是["B","C","D"]
            i:for (int i = 0;i<tempAnswers.length;i++){
                //想把答案也按照顺序放置 比如 ["B","C","D"] --》["","B","C","D"] 或者 ["A","","C","D"]  “”代表没有这个选项   为什么要这样呢？因为后面在计算code码 需要按照顺序获取
                int tempAnswerCode = tempAnswers[i].toCharArray()[0] - (int)'A';
                j:for (int j = 0;j< options.length;j++){
                    if (tempAnswerCode == j){
                        answers[j] = tempAnswers[i];
                        break j;
                    }
                }

            }
            String[] optionNumbers = new String[options.length];
            //答案对应的code码
            Integer[] answerCode = new Integer[options.length];
            for (int i = 0;i< options.length;i++){
                optionNumbers[i] = String.valueOf((char)((int)'A' + i));
            }
            i:for (int i=0;i<answerCode.length;i++){
                if (answers[i]!=null){
                    int temp = answers[i].toCharArray()[0] - (int)'A'; //["A","B","C"] ---》A
                    j:for (int j = 0;j<answerCode.length;j++){
                        if (temp == j){
                            answerCode[j] = (int)answers[i].toCharArray()[0] - (int)'A';
                            break j;
                        }
                    }
                }else {
                    answerCode[i] = -1;
                }
            }
            if (question.getLevel()!=null){
                questionVO.setLevel(question.getLevel());
            }
            questionVO.setNumber(currentQuestionNumber);
            questionVO.setSubject(question.getSubject());
            questionVO.setOptions(options);
            questionVO.setType(question.getType());
            questionVO.setAnswers(answers);
            questionVO.setAnswerCodes(answerCode);
            questionVO.setOptionNumber(optionNumbers);
        }else if (question.getType().equals("判断题")){
            if (question.getLevel()!=null){
                questionVO.setLevel(question.getLevel());
            }
            questionVO.setNumber(currentQuestionNumber);
            questionVO.setType(question.getType());
            questionVO.setSubject(question.getSubject());
            questionVO.setOptions(question.getOptions().split(CommonData.COMMON_SEPARATOR));
            questionVO.setAnswers(question.getAnswer().split(CommonData.COMMON_SEPARATOR));
        }else if (question.getType().equals("填空题")){
            if (question.getLevel()!=null){
                questionVO.setLevel(question.getLevel());
            }
            questionVO.setNumber(currentQuestionNumber);
            questionVO.setType(question.getType());
            questionVO.setSubject(question.getSubject());
            questionVO.setAnswers(question.getAnswer().split(CommonData.COMMON_SEPARATOR));
        }else if (question.getType().equals("综合题")){
            if (question.getLevel()!=null){
                questionVO.setLevel(question.getLevel());
            }
            questionVO.setNumber(currentQuestionNumber);
            questionVO.setType(question.getType());
            questionVO.setSubject(question.getSubject());
            questionVO.setAnswers(question.getAnswer().split(CommonData.COMMON_SEPARATOR));
        }
        return questionVO;
    }

    @Override
    public boolean templateSave(Template template) {
        Boolean result = null;
        try{
            templateDao.templateSave(template);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    public PageVO showTemplateList(int page, int count, Map<String, Object> map) {
        //首席我们利用mybatis提供的pageHelper   这里mybatis帮我们做了分页  所以我直接做查询过滤就行了
        PageHelper.startPage(page,count);
        List<Template> templates = templateDao.selectTemplateList(map);
        //因为前端需要展示模板所属的老师 和 模板分享给了那些老师 所以template中的属性不足以胜任  又不想更改实体类  所以写了一个新的类  需要做一下转换
        PageInfo pageInfo = new PageInfo(templates);
        PageVO pageVO = new PageVO();
        pageVO.setParams(pageInfo.getList());
        pageVO.setFilterName(map.get("name")+"-"+map.get("type")+"-"+map.get("course"));
        pageVO.setPage(pageInfo.getPageNum());
        pageVO.setCount(pageInfo.getPageSize());
        pageVO.setStart((page-1)*count);
        pageVO.setLength(count);
        pageVO.setRealPage(pageInfo.getPages());
        pageVO.setTotal((int)pageInfo.getTotal());
        pageVO.setPageStart(pageInfo.getPageNum());
        pageVO.setPageEnd(pageInfo.getPages());
        List<Template> templateList = (List<Template>) pageVO.getParams();
        List<TemplateVO> templateVOS = new ArrayList<>();
        for (Template template :templateList){
            TemplateVO templateVO = new TemplateVO();
            templateVO.setName(template.getName());
            templateVO.setTemplateType(template.getTemplateType());
            templateVO.setStatus(template.getStatus());
            templateVO.setTid(template.getTid());
            templateVO.setQuestion1(template.getQuestion1());
            templateVO.setQuestion2(template.getQuestion2());
            templateVO.setQuestion3(template.getQuestion3());
            templateVO.setQuestion4(template.getQuestion4());
            templateVO.setQuestion5(template.getQuestion5());
            templateVO.setId(template.getId());
            templateVO.setTotal_score(template.getTotal_score());
            templateVO.setCreate_time(template.getCreate_time());
            templateVO.setUpdate_time(template.getUpdate_time());
            templateVO.setYuliu1(template.getYuliu1());
            templateVO.setYuliu2(template.getYuliu2());
            templateVO.setYuliu3(template.getYuliu3());
            templateVO.setYuliu4(template.getYuliu4());
            //还需要查询模板所属老师
            templateVO.setRelationTeacher(templateDao.selectTeacherByTemplateId(template.getId()));
            //还需要查找当前模板分享给了那些老师
            templateVO.setShareToTeacher(templateDao.selectShareToTeacher(template.getId()));
            //这里我们利用的是java的逻辑获取数据  会有点性能慢 循环一次模板 与数据库交互多次  如果利用sql一次性查出这些信息就很快（但是sql会复杂 我现在有点懵）
            templateVOS.add(templateVO);
        }
        pageVO.setParams(templateVOS);
        return pageVO;
    }

    @Override
    public List<Teacher> selectShareTeacher(Long id) {
        return templateDao.selectShareTeacher(id);
    }

    @Override
    public Template selectTemplateById(Long id) {
        return templateDao.selectTemplateById(id);
    }

    @Override
    public int selectTemplateShareCount(Long id) {
        return templateDao.selectTemplateShareStatusById(id);
    }

    @Override
    public void deleteOneList(Long id) {
        templateDao.deleteOneList(id);
    }

    @Override
    public int setTemplateStatus(Map<String, Object> map) {
        return templateDao.updateTemplateStatusById(map);
    }

    @Override
    public void setShareStatus(Long templateId, String teacherIds) {
        String[] ids = teacherIds.split(",");
        for (int i = 0;i< ids.length;i++){
            try{
                templateDao.setShareStatus(templateId,Long.parseLong(ids[i]));
            }catch (Exception e){
                //可能会出现重复关系
            }
        }
    }

    @Override
    public TemplateVO reverseVO(Template template) {
        TemplateVO templateVO = new TemplateVO();
        templateVO.setTemplateType(template.getTemplateType());
        templateVO.setId(template.getId());
        templateVO.setName(template.getName());
        templateVO.setYuliu1(template.getYuliu1());
        templateVO.setTotal_score(template.getTotal_score());
        {
            String question1 = template.getQuestion1();
            String[] question1List = question1.split(CommonData.COMMON_SEPARATOR);
            templateVO.setQuestion1_score(Integer.parseInt(question1List[0]));
            ArrayList<Integer> tempQuestion1List = new ArrayList<>();
            for (int i = 1; i < question1List.length; i++) {
                tempQuestion1List.add(Integer.parseInt(question1List[i]));
            }
            templateVO.setQuestion1_count(tempQuestion1List);
        }
        {
            String question2 = template.getQuestion2();
            String[] question2List = question2.split(CommonData.COMMON_SEPARATOR);
            templateVO.setQuestion2_score(Integer.parseInt(question2List[0]));
            ArrayList<Integer> tempQuestion2List = new ArrayList<>();
            for (int i = 1; i < question2List.length; i++) {
                tempQuestion2List.add(Integer.parseInt(question2List[i]));
            }
            templateVO.setQuestion2_count(tempQuestion2List);
        }
        {
            String question3 = template.getQuestion3();
            String[] question3List = question3.split(CommonData.COMMON_SEPARATOR);
            templateVO.setQuestion3_score(Integer.parseInt(question3List[0]));
            ArrayList<Integer> tempQuestion3List = new ArrayList<>();
            for (int i = 1; i < question3List.length; i++) {
                tempQuestion3List.add(Integer.parseInt(question3List[i]));
            }
            templateVO.setQuestion3_count(tempQuestion3List);
        }
        {
            String question4 = template.getQuestion4();
            String[] question4List = question4.split(CommonData.COMMON_SEPARATOR);
            templateVO.setQuestion4_score(Integer.parseInt(question4List[0]));
            ArrayList<Integer> tempQuestion4List = new ArrayList<>();
            for (int i = 1; i < question4List.length; i++) {
                tempQuestion4List.add(Integer.parseInt(question4List[i]));
            }
            templateVO.setQuestion4_count(tempQuestion4List);
        }
        {
            String question5 = template.getQuestion5();
            String[] question5List = question5.split(CommonData.COMMON_SEPARATOR);
            templateVO.setQuestion5_score(Integer.parseInt(question5List[0]));
            ArrayList<Integer> tempQuestion5List = new ArrayList<>();
            for (int i = 1; i < question5List.length; i++) {
                tempQuestion5List.add(Integer.parseInt(question5List[i]));
            }
            templateVO.setQuestion5_count(tempQuestion5List);
        }
        return templateVO;
    }

    @Override
    public boolean editSave(Template template) {
        Boolean result = null;
        try{
            templateDao.editSave(template);
            result = true;
        }catch (Exception e){
            result = false;
        }

        return result;
    }

    @Override
    public void updateTemplateRelationQuestionStatus(List<Long> questionIds,String status) {
        templateDao.updateTemplateRelationQuestionStatus(questionIds,status);
    }
}
